﻿using System;
using System.Collections.Generic;
using System.Text;
using DataBaseConnect.IRepositories;
using Entity;
using Utils;

namespace DataBaseConnect.App
{
    public class DepartmentAppService : IDepartmentAppService
    {
        private readonly IDepartmentRepository _departmentRepository;

        public DepartmentAppService(IDepartmentRepository departmentRepository)
        {
            _departmentRepository = departmentRepository;
        }

        /// <summary>
        /// 获取所有部门&&查询
        /// </summary>
        /// <returns></returns>
        public List<Department> GetDepartmentList(string DepartmentName)
        {
            if (!string.IsNullOrEmpty(DepartmentName))
            {
                return _departmentRepository.GetAllList(f => f.DepartmentName == DepartmentName);
            }
            else
            {
                return _departmentRepository.GetAllList();
            }
        }

        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="Input">部门信息实体</param>
        /// <returns></returns>
        public ReceiveStatus AddDepartmentMsg(Department Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _departmentRepository.Insert(Input, true);
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据主键获取部门信息
        /// </summary>
        /// <param name="UserId">主键</param>
        /// <returns></returns>
        public Department GetDepartmentFirst(int Id)
        {
            return _departmentRepository.FirstOrDefault(f => f.Id == Id);
        }

        /// <summary>
        /// 编辑部门信息
        /// </summary>
        /// <param name="Input">部门信息实体</param>
        /// <returns></returns>
        public ReceiveStatus EditDepartmentMsg(Department Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _departmentRepository.Update(Input);
                receiveStatus.msg = "编辑成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }
    }
}
